<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\Goods\Mapper;

use App\Goods\Model\GoodsSku;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 商品规格Mapper类
 */
class GoodsSkuMapper extends AbstractMapper
{
    /**
     * @var GoodsSku
     */
    public $model;

    public function assignModel()
    {
        $this->model = GoodsSku::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 记录ID
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 商品sku唯一标识 (由规格id组成)
        if (isset($params['goods_sku_id']) && filled($params['goods_sku_id'])) {
            $query->where('goods_sku_id', 'like', '%'.$params['goods_sku_id'].'%');
        }

        // 商品ID
        if (isset($params['goods_id']) && filled($params['goods_id'])) {
            $query->where('goods_id', '=', $params['goods_id']);
        }

        // 规格图片ID
        if (isset($params['image_id']) && filled($params['image_id'])) {
            $query->where('image_id', '=', $params['image_id']);
        }

        // 商品sku编码
        if (isset($params['goods_sku_no']) && filled($params['goods_sku_no'])) {
            $query->where('goods_sku_no', 'like', '%'.$params['goods_sku_no'].'%');
        }

        // 商品价格
        if (isset($params['goods_price']) && filled($params['goods_price'])) {
            $query->where('goods_price', '=', $params['goods_price']);
        }

        // 商品划线价
        if (isset($params['line_price']) && filled($params['line_price'])) {
            $query->where('line_price', '=', $params['line_price']);
        }

        // 当前库存数量
        if (isset($params['stock_num']) && filled($params['stock_num'])) {
            $query->where('stock_num', '=', $params['stock_num']);
        }

        // 商品重量(Kg)
        if (isset($params['goods_weight']) && filled($params['goods_weight'])) {
            $query->where('goods_weight', '=', $params['goods_weight']);
        }

        // SKU的规格属性(json格式)
        if (isset($params['goods_props']) && filled($params['goods_props'])) {
            $query->where('goods_props', 'like', '%'.$params['goods_props'].'%');
        }

        // 规格值ID集(json格式)
        if (isset($params['spec_value_ids']) && filled($params['spec_value_ids'])) {
            $query->where('spec_value_ids', 'like', '%'.$params['spec_value_ids'].'%');
        }

        // 商城ID
        if (isset($params['store_id']) && filled($params['store_id'])) {
            $query->where('store_id', '=', $params['store_id']);
        }

        // 创建时间
        if (isset($params['create_time']) && filled($params['create_time'])) {
            $query->where('create_time', '=', $params['create_time']);
        }

        // 更新时间
        if (isset($params['update_time']) && filled($params['update_time'])) {
            $query->where('update_time', '=', $params['update_time']);
        }

        return $query;
    }
}